package com.redhat.auth.application.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.dayatang.domain.InstanceFactory;
import com.dayatang.querychannel.service.QueryChannelService;
import com.dayatang.querychannel.support.Page;
import com.redhat.auth.application.FunctionEntityApplication;
import com.redhat.auth.core.AuthResourceBundleI18nService;
import com.redhat.auth.domain.FunctionEntity;
import com.redhat.auth.domain.FunctionEntity.Status;

/**
 * 功能实体应用（业务逻辑）层
 *<br />
 * @author vakin.Jiang
 *2010-4-26
 */
@Service
public class FunctionEntityApplicationImpl implements FunctionEntityApplication {

	private static final Logger logger = LoggerFactory
			.getLogger(FunctionEntityApplicationImpl.class);

	//查询通道
	protected static QueryChannelService queryChannel;

	protected static QueryChannelService queryChannel() {
		if (queryChannel == null) {
			queryChannel = InstanceFactory
					.getInstance(QueryChannelService.class);
		}
		return queryChannel;
	}

	public List<FunctionEntity> queryChildNode(Long id, int currentPage,
			int pageSize) {
		FunctionEntity entity = new FunctionEntity();
		entity.setId(id);
		return entity.getChild();
	}

	public FunctionEntity createFunctionEntity(FunctionEntity entity) {

		// 如果未指定父实体则设置默认父实体为root节点ID
		if (entity.getParent() == null || entity.getParent().getId() == null) {
			FunctionEntity parent = getDefaultRootFunctionEntity();
			entity.setParent(parent);
		}

		return entity.create();
	}

	public void deleteFunctionEntityById(Long id) {

		//查找关联子实体
		FunctionEntity entity = getFunctionEntityById(id);
		if (entity == null)
			return;
		entity.delete();
	}

	public Long getCount(FunctionEntity search) {
		StringBuffer hql = new StringBuffer();
		Object[] obj = createQueryParams(search, hql);
		return queryChannel().queryResultSize(hql.toString(), obj);
	}

	public FunctionEntity getFunctionEntityById(Long id) {
		FunctionEntity entity = FunctionEntity.get(id);
		if (entity == null)
			throw new RuntimeException(AuthResourceBundleI18nService
					.getMessage("auth.functionentitytype.entityTypeIsNotExist",
							"当前实体类型不存在"));
		return entity;
	}

	public List<FunctionEntity> queryAllByPage(FunctionEntity search,
			int currentPage, int pageSize) {
		StringBuffer hql = new StringBuffer();
		Object[] obj = createQueryParams(search, hql);
		Page<FunctionEntity> page = queryChannel().queryPagedResult(
				hql.toString(), obj, currentPage, pageSize);
		return page == null ? null : page.getResult();
	}

	public FunctionEntity updateFunctionEntity(FunctionEntity entity) {
		return entity.update();
	}

	public void setRelevance(long rscId, long destId) {

	}

	public void changStatus(Long id, Status status) {
		FunctionEntity funEntity = getFunctionEntityById(id);
		if (Status.ENABLED.equals(status)) {
			funEntity.enable();
		} else if (Status.DISABLED.equals(status)) {
			funEntity.disable();
		}
	}

	public FunctionEntity getDefaultRootFunctionEntity() {

		FunctionEntity dafaultNode = null;

		List<FunctionEntity> rootNodes = queryChannel().queryResult(
				"from FunctionEntity f where f.parent is NULL", new Object[0]);

		if (rootNodes == null || rootNodes.isEmpty()) {
			dafaultNode = new FunctionEntity();
			dafaultNode.setParent(null);
			dafaultNode.setAlias(" ");
			dafaultNode.setName("root");
			dafaultNode.create();

		} else {
			dafaultNode = rootNodes.get(0);
		}

		return dafaultNode;
	}

	/**
	 * 构造查询条件
	 * @param search
	 * @param hql
	 * @return
	 */
	private Object[] createQueryParams(FunctionEntity search, StringBuffer hql) {
		hql.append("select f from FunctionEntity f");
		List<Object> params = new ArrayList<Object>();
		if (search != null) {
			if (search.getFunctionEntityType() != null) {
				hql.append(" join f.functionEntityType type where type.id=?");
				params.add(search.getFunctionEntityType().getId());
			} else {
				hql.append(" where 1=1");
			}

			if (StringUtils.isNotBlank(search.getName())) {
				hql.append(" and f.name like ?");
				params.add("%" + search.getName() + "%");
			}

			if (StringUtils.isNotBlank(search.getAlias())) {
				hql.append(" and f.name like ?");
				params.add("%" + search.getAlias() + "%");
			}

			if (search.getStatus() != null) {
				hql.append(" and f.status=?");
				params.add(search.getStatus());
			}
		}

		Object[] obj = params.isEmpty() ? new Object[] {} : params.toArray();
		return obj;
	}

}
